package cn.iocoder.yudao.module.crossborder.platform.service.platformauth;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformauth.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformauth.PlatformAuthDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformauth.PlatformAuthMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商平台授权信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformAuthServiceImpl implements PlatformAuthService {

    @Resource
    private PlatformAuthMapper platformAuthMapper;

    @Override
    public Long createPlatformAuth(PlatformAuthSaveReqVO createReqVO) {
        // 插入
        PlatformAuthDO platformAuth = BeanUtils.toBean(createReqVO, PlatformAuthDO.class);
        platformAuthMapper.insert(platformAuth);

        // 返回
        return platformAuth.getId();
    }

    @Override
    public void updatePlatformAuth(PlatformAuthSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformAuthExists(updateReqVO.getId());
        // 更新
        PlatformAuthDO updateObj = BeanUtils.toBean(updateReqVO, PlatformAuthDO.class);
        platformAuthMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformAuth(Long id) {
        // 校验存在
        validatePlatformAuthExists(id);
        // 删除
        platformAuthMapper.deleteById(id);
    }

    @Override
        public void deletePlatformAuthListByIds(List<Long> ids) {
        // 删除
        platformAuthMapper.deleteByIds(ids);
        }


    private void validatePlatformAuthExists(Long id) {
        if (platformAuthMapper.selectById(id) == null) {
            throw exception(PLATFORM_AUTH_NOT_EXISTS);
        }
    }

    @Override
    public PlatformAuthDO getPlatformAuth(Long id) {
        return platformAuthMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformAuthDO> getPlatformAuthPage(PlatformAuthPageReqVO pageReqVO) {
        return platformAuthMapper.selectPage(pageReqVO);
    }

    @Override
    public PlatformAuthDO getPlatformAuthByStoreId(Long storeId) {
        return platformAuthMapper.selectByStoreId(storeId);
    }

    @Override
    public void updateTokens(Long storeId, String accessToken, String refreshToken, java.time.LocalDateTime expireTime) {
        PlatformAuthDO auth = getPlatformAuthByStoreId(storeId);
        if (auth != null) {
            PlatformAuthDO updateObj = new PlatformAuthDO();
            updateObj.setId(auth.getId());
            updateObj.setAccessToken(accessToken);
            updateObj.setRefreshToken(refreshToken);
            updateObj.setTokenExpireTime(expireTime);
            updateObj.setLastRefreshTime(java.time.LocalDateTime.now());
            updateObj.setAuthStatus(1); // 已授权
            platformAuthMapper.updateById(updateObj);
        }
    }

    @Override
    public void updateAuthStatus(Long storeId, Integer authStatus) {
        PlatformAuthDO auth = getPlatformAuthByStoreId(storeId);
        if (auth != null) {
            PlatformAuthDO updateObj = new PlatformAuthDO();
            updateObj.setId(auth.getId());
            updateObj.setAuthStatus(authStatus);
            platformAuthMapper.updateById(updateObj);
        }
    }

    @Override
    public List<PlatformAuthDO> getExpiredAuthList(java.time.LocalDateTime expireTime) {
        return platformAuthMapper.selectExpiredAuth(expireTime);
    }

}